package com.xt.yncu.approve.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.xt.base.dao.hibernate.BaseDaoHibernate;
import com.xt.base.service.BaseService;
import com.xt.base.wireless.server.MobileActionUtil;
import com.xt.yncu.approve.dao.ApproveDao;
import com.xt.yncu.html.XmlParseHtml;
import com.xt.yncu.soap.SOAPInterface;
import com.xt.yncu.util.Config;
import com.xt.yncu.util.OfficeOther;

/**
 * <br>
 * <br>
 * 
 * <p>
 * <a href="ApproveService.java.html"><i>查看源文件</i></a>
 * </p>
 * 
 * @author 杨洪波
 * @create-date:2011-11-3
 */
public class ApproveService extends BaseService {
	private static final long serialVersionUID = -203936629027343244L;

	private SOAPInterface soapInterface;
	private XmlParseHtml xmlHtml;
	private ApproveDao approveDAO;
	private Config config;
	/*private ThreadPool threadPool;
	
	private HttpPostUtil httpPost;*/
	private OfficeOther other = new OfficeOther();
	private StringBuilder urlBuilder;
	private boolean isKJava = false;
	List<String> path;
	List<String> info;
	Map<String, Object> map2;

	public String queryApproveList(Map<String, Object> map) {
		dealDbPath(map);
		return soapInterface.send(map);
	}

	public String getApproveInfo(Map<String, Object> map) {
		if (map.get("isKJava")!=null) {
			isKJava=true;
			map.remove(isKJava);
		}
		xmlHtml.setOs(map.get("os")==null?null:map.get("os").toString());

		dealDbPath(map);
		
		
		map2 = map;
		// Map<String,Object> map = MobileActionUtil.parseRequestXml(xml);
		xmlHtml.setKJava(isKJava);
		
		String htmXml = xmlHtml.mobileNewXml(soapInterface.send(map), String
				.valueOf(map.get("funcid")));
		log.info("经过解析后的app_show_3.xml文件内容：\n" + htmXml);
		/*if (!isKJava) {
			path = xmlHtml.getFileList();
			info = xmlHtml.getFileInfoid();
			config.setCreateHtmlName(xmlHtml.getCreateHtmlName());
			if (config.remote) {
				try {
					new Thread() {
						public void run() {
							urlBuilder = new StringBuilder();
							urlBuilder.append(config.getHttpUrl()).append(
									config.getHttpAction());
							httpPost = new HttpPostUtil(urlBuilder.toString());
							httpPost.addTextParameter("path", other
									.listToString(path));
							httpPost.addTextParameter("info", other
									.listToString(info));
							httpPost.addTextParameter("universalid", xmlHtml
									.getUniversalid());
							httpPost.addTextParameter("createHtmlName", config
									.getCreateHtmlName());
							httpPost.addTextParameter("xmlMap",
									MobileActionUtil.parseResultMap2Xml(map2));
							httpPost.send();
							httpPost.clearAllParameters();
						}
					}.start();
				} catch (Exception e) {
					log
							.error(
									"Java用Http以Post方请求数据出错(com/xt/yncu/approve/service/ApproveService.java第72行至84行)",
									e);
				}
			} else {
				log.info("approveservice中，准备附件解析---！");
				FileDown fileDown = new FileDown(path, info, xmlHtml
						.getUniversalid(), map, soapInterface, config);
				threadPool.addThread(fileDown);
			}
		}*/
		return htmXml;
	}
	

	public String saveOrUpdate(Map<String, Object> map) {
		String xmlString = null, xml = MobileActionUtil.parseResultMap2Xml(map);
		// Map<String,Object> map = MobileActionUtil.parseRequestXml(xml);
		long date = new Date().getTime();
		map.put("date", date);
		String randomid = map.get("userid") + String.valueOf(date);
		map.put("randomid", randomid);
		String s = String.valueOf(map.get("comment"));
		s.replaceAll(" \r\n", " ");
		map.put("comment", "<![CDATA[" + s + "]]>");

//		Approve approve = new Approve();
//		approve.setUserId(String.valueOf(map.get("userid")));
//		approve.setPassword("");
//		approve.setXml(xml);
//		approve.setSubmitDate(new Date());
//		approveDAO.save(approve);
		try {
			dealDbPath(map);
			xmlString = soapInterface.send(map);
//			if (XmlParse.isXml(xml)) {
//				Map<String, Object> map2 = MobileActionUtil
//						.parseRequestXml(xmlString);
//
//				if (XmlParse.isException(xml)) {
//					approve.setSubmitResult(false);
//					approve.setExceptionInfo(xmlString);
//				} else if ("false".equals(map.get("success"))) {
//					approve.setSubmitResult(false);
//					approve.setExceptionInfo(xmlString);
//				} else {
//					approve.setSubmitResult(true);
//					approve.setSubmitResultInfo(xmlString);
//				}
//			}
//			approveDAO.save(approve);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return xmlString;
	}

	private void dealDbPath(Map<String,Object> map){
		//处理待办事务的知会的dbpath
		log.info("###传入的funcid="+map.get("funcid")+",,code="+map.get("code"));
		if("D002".equals(map.get("funcid"))&&"2".equals(map.get("code"))){
			//map.put("dbpath", "softoon\\WorkflowSWCL\\WorkFlow\\WorkFlow.nsf");
		}
	}
	
	@Override
	public BaseDaoHibernate getDao() {
		return approveDAO;
	}

	public void setApproveDAO(ApproveDao approveDAO) {
		this.approveDAO = approveDAO;
	}

	public void setSoapInterface(SOAPInterface soapInterface) {
		this.soapInterface = soapInterface;
	}

	public XmlParseHtml getXmlHtml() {
		return xmlHtml;
	}

	public void setXmlHtml(XmlParseHtml xmlHtml) {
		this.xmlHtml = xmlHtml;
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	/*public ThreadPool getThreadPool() {
		return threadPool;
	}

	public void setThreadPool(ThreadPool threadPool) {
		this.threadPool = threadPool;
	}*/
}
